<?php
/**
 * 这是一个从字符串解析XML的示例，将字符串加载为XML后存储为SimpleXML对象
 * User: ford
 * Date: 15-7-9
 * Time: 下午3:43
 */

$xml = <<<THE_XML
<animal>
	<type>dog</type>
	<name>snoopy</name>
</animal>
THE_XML;

//为字符串加载到SimpleXMLElement对象
$xml_object = simplexml_load_string($xml);

foreach ($xml_object as $element => $value) {
	echo $element . ":" . $value . "\n";
}

//又比如，稍复杂一点
$xml = <<<THE_XML2
<animals>
	<dog>
		<name>snoopy</name>
		<color>brown</color>
		<breed>beagle cross</breed>
	</dog>
	<cat>
		<name>teddy</name>
		<color>brown</color>
		<breed>tabby</breed>
	</cat>
	<dog>
		<name>jade</name>
		<color>black</color>
		<breed>lab cross</breed>
	</dog>
</animals>
THE_XML2;

$xml_object = simplexml_load_string($xml);

foreach ($xml_object->dog as $dog) {
	echo $dog->name . "\n";

}

